package org.example.Controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.example.Entity.Category;
import org.example.Entity.Dish;
import org.example.Entity.DishFlavor;
import org.example.Mapper.DishMapper;
import org.example.common.R;
import org.example.dto.DishDto;
import org.example.service.DishService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 菜品管理分页查询
     * @param page
     * @param pageSize
     * @param name 菜品名称
     * @return
     */
    @GetMapping("page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        Page<DishDto> pageInfo = new Page<>(page,pageSize);

        MPJLambdaWrapper<Dish> dishMPJLambdaWrapper = new MPJLambdaWrapper<>();
        dishMPJLambdaWrapper.selectAll(Dish.class)
                .selectAs(Category::getName,"categoryName")
                .like(StringUtils.isNotEmpty(name),Dish::getName,name)
                .orderByAsc(Dish::getSort)
                .leftJoin(Category.class,Category::getId,Dish::getCategoryId);
        IPage<DishDto> dishDtoIPage = dishMapper.selectJoinPage(pageInfo,DishDto.class,dishMPJLambdaWrapper);
        return R.success(pageInfo);
    }

    /**
     * 新增菜品
     * @param dish
     * @return
     */
    @PostMapping()
    //使用事务保证数据一致性
    @Transactional
    public R<String> save(@RequestBody DishDto dish){
        log.info("dishDto={}",dish);
        dishService.saveWithFlavor(dish);
        return R.success("新增菜品成功");
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){

        MPJLambdaWrapper<Dish> dishMPJLambdaWrapper = new MPJLambdaWrapper<>();
        dishMPJLambdaWrapper.selectAll(Dish.class)
                .selectCollection(DishFlavor.class,DishDto::getFlavors)
                .leftJoin(DishFlavor.class,DishFlavor::getDishId,Dish::getId)
                .eq(Dish::getId,id);
        DishDto dishDto = dishService.selectJoinOne(DishDto.class, dishMPJLambdaWrapper);
        return R.success(dishDto);
    }


    /**
     * 修改菜品
     * @param dish
     * @return
     */
    @PutMapping("")
    @Transactional
    public R<String> update(@RequestBody DishDto dish){
        log.info("dishDto={}",dish);
        dishService.updateWithFlavor(dish);
        return R.success("修改菜品成功");
    }

    @GetMapping("list")
    public R<List<Dish>> list(Dish dish){
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId())
                .eq(Dish::getStatus,1)
                .orderByDesc(Dish::getSort)
                .orderByDesc(Dish::getUpdateTime);
        return R.success(dishService.list(queryWrapper));
    }

    /**
     * 批量修改菜品状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        log.info("修改菜品状态，ids：{}",ids);
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Dish::getStatus,status)
                .in(Dish::getId,ids);
        dishService.update(updateWrapper);
        return R.success("修改成功！");
    }


    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping()
    @Transactional
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除套餐，ids：{}",ids);
        dishService.removeDishes(ids);
        return R.success("删除成功！");
    }

}
